/**
 * 
 */
package com.xwtech.cring.admin.controller;

import java.sql.SQLException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.xwtech.cms.data.AbstractDataAdapter;
import com.xwtech.cms.data.db.JdbcTransaction;
import com.xwtech.cring.admin.bo.BgUserBO;
import com.xwtech.cring.admin.pub.AdminUserRetConst;
import com.xwtech.framework.pub.po.FrameLogin;
import com.xwtech.framework.pub.result.ResultConstants;
import com.xwtech.framework.pub.result.ResultInfo;
import com.xwtech.framework.pub.result.ResultInfos;
import com.xwtech.framework.pub.utils.StringUtils;
import com.xwtech.framework.pub.web.RequestNameConstants;

/**
 * @author Administrator
 * 
 */
public class BgUserModifyPwdController extends MultiActionController {

	protected static final Logger logger = Logger
			.getLogger(BgUserEditDelController.class);

	private BgUserBO bgUserBO;

	public BgUserBO getBgUserBO() {
		return bgUserBO;
	}

	public void setBgUserBO(BgUserBO bagUserBO) {
		this.bgUserBO = bagUserBO;
	}

	public ModelAndView updateBgUser(HttpServletRequest request,
			HttpServletResponse response) {
		HashMap map = new HashMap();
		ResultInfos resultInfos = new ResultInfos();
		map.put(RequestNameConstants.RESULTINFOS, resultInfos);

		String query = request.getParameter("_query");
		String adapter = request.getParameter("_adapter");
		String url = request.getParameter("_url");

		String login_name = request.getParameter("login_name");
		FrameLogin frameLogin = bgUserBO.getFrameLogin(login_name);

		String password = StringUtils.hashKey("666666", "MD5");
		String loginPwdOld = request.getParameter("loginPwdOld");
		String loginPwdNew1 = request.getParameter("loginPwdNew1");

		if (loginPwdOld != null && loginPwdOld.length() > 0) {
			String loginPwdOldChange = StringUtils.hashKey(loginPwdOld, "MD5");
			logger.info("查询用户[" + frameLogin.getLoginName() + "]加密后，密码["
					+ loginPwdOldChange + "].");
			if (loginPwdOldChange.equals(frameLogin.getLoginPwd())) {
				password = StringUtils.hashKey(loginPwdNew1, "MD5");
			} else {
				resultInfos.add(new ResultInfo(AdminUserRetConst.OLD_PASSWORD_ERROR));
				resultInfos.setGotoUrl("/cring/jsp/admin/bgUsermodifyPwd.jsp");
				return new ModelAndView("/cring/jsp/admin/information.jsp",
						RequestNameConstants.INFORMATION, map);
			}
		} else {
			password = frameLogin.getLoginPwd();
		}
		request.setAttribute("password", password);
		AbstractDataAdapter ada = AbstractDataAdapter.getDataAdapter(request
				.getSession().getServletContext(), adapter);

		int ret = -1;
		// 对得到的xml路径字符串分组
		String[] xmlString = query.split(",");
		logger.debug("共有sql-xml配置文件" + xmlString.length + "个");
		if (xmlString.length > 1) {
			logger.debug("需要进行事务处理...");
			JdbcTransaction transaction = ada.getTransaction();
			try {
				for (int i = 0; i < xmlString.length; i++) {
					ret = transaction.update(xmlString[i], request);
				}
				transaction.commit();
			} catch (Exception e) {
				logger.error("数据库操作时出错！", e);
				try {
					transaction.rollback();
				} catch (SQLException e1) {
					logger.error("数据库操作时出错！", e);
					ret = -1;
				}
				ret = -1;
			}
		}
		if (ret < 0) {
			resultInfos.add(new ResultInfo(AdminUserRetConst.PASSWORD_UPDATE_FAIL));
		} else {
			resultInfos.add(new ResultInfo(AdminUserRetConst.PASSWORD_UPDATE_SUCCESS));
		}
		resultInfos.setGotoUrl(url);
		return new ModelAndView("/cring/jsp/admin/information.jsp",
				RequestNameConstants.INFORMATION, map);
	}
}
